package com.bcmusic.music.controller;

import com.bcmusic.music.config.TokenDecode;
import com.bcmusic.music.domain.music.dto.UploadMusicDTO;
import com.bcmusic.music.domain.music.vo.CertificateVO;
import com.bcmusic.music.domain.music.vo.DownLoadMusicVO;
import com.bcmusic.music.domain.music.vo.UpLoadedMusicVO;
import com.bcmusic.music.pojo.Music;
import com.bcmusic.music.service.MusicService;
import com.github.pagehelper.PageInfo;
import entity.AllowType;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(tags = "音乐模块")
@RestController
@RequestMapping("/music")
public class MusicController {

    @Autowired
    private MusicService musicService;

    @Autowired
    private TokenDecode tokenDecode;


//    /***
//     * Music分页搜索实现
//     * @param page:当前页
//     * @param size:每页显示多少条
//     * @return
//     */
//    @ApiOperation(value = "Music分页查询",notes = "分页查询Music方法详情",tags = {"MusicController"})
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
//            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
//    })
//    @GetMapping(value = "/search/{page}/{size}" )
//    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
//        //调用MusicService实现分页查询Music
//        PageInfo<Music> pageInfo = musicService.findPage(page, size);
//        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
//    }
//
//    /***
//     * 多条件搜索品牌数据
//     * @param music
//     * @return
//     */
//    @ApiOperation(value = "Music条件查询",notes = "条件查询Music方法详情",tags = {"MusicController"})
//    @PostMapping(value = "/search" )
//    public Result<List<Music>> findList(@RequestBody(required = false) @ApiParam(name = "Music对象",value = "传入JSON数据",required = false) Music music){
//        //调用MusicService实现条件查询Music
//        List<Music> list = musicService.findList(music);
//        return new Result<List<Music>>(true,StatusCode.OK,"查询成功",list);
//    }
//
//    /***
//     * 根据ID删除品牌数据
//     * @param id
//     * @return
//     */
//    @ApiOperation(value = "Music根据ID删除",notes = "根据ID删除Music方法详情",tags = {"MusicController"})
//    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
//    @DeleteMapping(value = "/{id}" )
//    public Result delete(@PathVariable Integer id){
//        //调用MusicService实现根据主键删除
//        musicService.delete(id);
//        return new Result(true,StatusCode.OK,"删除成功");
//    }
//
//    /***
//     * 修改Music数据
//     * @param music
//     * @param id
//     * @return
//     */
//    @ApiOperation(value = "Music根据ID修改",notes = "根据ID修改Music方法详情",tags = {"MusicController"})
//    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
//    @PutMapping(value="/{id}")
//    public Result update(@RequestBody @ApiParam(name = "Music对象",value = "传入JSON数据",required = false) Music music, @PathVariable Integer id){
//        //设置主键值
//        music.setMusicId(id);
//        //调用MusicService实现修改Music
//        musicService.update(music);
//        return new Result(true,StatusCode.OK,"修改成功");
//    }
//
//    /***
//     * 新增Music数据
//     * @param music
//     * @return
//     */
//    @ApiOperation(value = "Music添加",notes = "添加Music方法详情",tags = {"MusicController"})
//    @PostMapping
//    public Result add(@RequestBody  @ApiParam(name = "Music对象",value = "传入JSON数据",required = true) Music music){
//        //调用MusicService实现添加Music
//        musicService.add(music);
//        return new Result(true,StatusCode.OK,"添加成功");
//    }
//
//    /***
//     * 根据ID查询Music数据
//     * @param id
//     * @return
//     */
//    @ApiIgnore
//    @GetMapping("/findById")
//    public Result<Music> findById(@RequestParam Integer id){
//        //调用MusicService实现根据主键查询Music
//        Music music = musicService.findById(id);
//        return new Result<Music>(true,StatusCode.OK,"查询成功",music);
//    }
//
    /***
     * 查询Music全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Music",notes = "查询所Music有方法详情",tags = {"MusicController"})
    @GetMapping("findAll")
    public Result<List<Music>> findAll(){
        //调用MusicService实现查询所有Music
        List<Music> list = musicService.findAll();
        return new Result<List<Music>>(true, StatusCode.OK,"查询成功",list) ;
    }

    /**
     * 上传音乐
     * @param music
     * @return
     */
    @ApiOperation(value = "用户上传音乐",notes = "用户上传音乐",tags = {"MusicController"})
    @PostMapping("uploadMusic")
    public Result uploadMusic(UploadMusicDTO music){
        int id = musicService.uploadMusic(music, tokenDecode.getUserInfo().get("id"));
        if(id==0)
        {
            return new Result(true,StatusCode.ERROR,"上传失败,请检查各种信息");
        }
        else {
            return new Result(true,StatusCode.OK,"上传成功");
        }
    }

    @ApiOperation(value = "上传文件",notes = "上传文件",tags = {"MusicController"})
    @PostMapping("uploadFile")
    public Result uploadFile(@RequestPart("file")MultipartFile file){
        String s = musicService.uploadFile(file);
        if(s==null){
            return new Result(true,StatusCode.ERROR,"上传失败");
        }
        return new Result(true,StatusCode.OK,"上传成功",s);
    }


    /**
     * 获取上传音乐
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Music条件分页查询",notes = "分页条件查询Music方法详情",tags = {"MusicController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/getUploadedMusic/{page}/{size}" )
    public Result<PageInfo<UpLoadedMusicVO>> getUploadedMusic(@PathVariable  int page, @PathVariable  int size){
        //调用MusicService实现分页查询Music
        String userNum=tokenDecode.getUserInfo().get("id");
        PageInfo<UpLoadedMusicVO> pageInfo = musicService.findPage(page, size,userNum);
        return new Result<>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /**
     * 撤销音乐，目前暂定审核通过不可以撤销，后面改
     * @param musicId
     * @return
     */
    @ApiOperation(value = "撤销音乐上传",notes = "撤销音乐上传",tags = {"MusicController"})
    @GetMapping("/backout")
    public Result backout(@RequestParam @ApiParam(name = "musicId",value = "音乐id",required = true) Integer musicId){
        String userNum=tokenDecode.getUserInfo().get("id");
        int i = musicService.backOutMusic(musicId, userNum);
        if(i==0){
            return new Result(true,StatusCode.ERROR,"撤销失败");
        }else {
            return new Result(true,StatusCode.OK,"撤销成功");
        }
    }

    @ApiOperation(value = "获取一首审核过的音乐证书",notes = "获取一首审核过的音乐证书",tags = {"MusicController"})
    @GetMapping("/getCertificate")
    public Result<CertificateVO> getCertificateVO(@RequestParam @ApiParam(name = "musicId",value = "音乐id",required = true) Integer musicId){
        String userNum=tokenDecode.getUserInfo().get("id");
        CertificateVO certificateVO = musicService.getCertificateVO(musicId, userNum);
        if(certificateVO==null){
            return new Result<>(true,StatusCode.ERROR,"查询失败，您还没有认证");
        }
        return new Result<>(true,StatusCode.OK,"查询成功",certificateVO);
    }

    /**
     * 下载音乐
     * @param musicId
     * @return
     */
    @ApiOperation(value = "下载音乐",notes = "下载音乐",tags = {"MusicController"})
    @GetMapping("/downLoadMusic")
    public Result<String>downLoadMusic(@RequestParam @ApiParam(name = "musicId",value = "音乐id",required = true) Integer musicId){
        String userNum=tokenDecode.getUserInfo().get("id");
        DownLoadMusicVO downLoadMusicVO = musicService.downLoadMusic(musicId, userNum);
        if(downLoadMusicVO==null){
            return new Result<>(true,StatusCode.ERROR,"获取音乐失败，请联系管理员");
        }
        return new Result<>(true,StatusCode.OK,"获取下载链接成功",downLoadMusicVO);
    }


    /**
     * 内部调用根据音乐id查找音乐列表
     */
    @ApiIgnore
    @GetMapping("/getMusicByIdList")
    public Result<List<Music>> getMusicByIdList(@RequestParam("idList") List<Integer>idList){
        List<Music> musicByIdList = musicService.getMusicByIdList(idList);
        return new Result<>(true,StatusCode.OK,"查询成功",musicByIdList);
    }

}
